package com.example.experiment3;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class DataProcessingActivity extends AppCompatActivity {
    public static String TAG = "DataProcessing";

    private String statement = "Android 的四大组件分别是 Activity，（     ），BroadcastReceiver 和 ContentProvider";
    private String answer = "Service";
    private int difficulty = 1;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_data_processing);
        path = getExternalFilesDir(null).toString()
                + File.separator + "exam"
                + File.separator + "json.txt";
    }

    public void save(View view) {
        JSONObject temp = new JSONObject();      // 创建一个新的JSONObject对象
        try {
            temp.put("statement", statement);
            temp.put("answer", answer);
            temp.put("difficulty", difficulty);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        // 判断是否存在SD卡
        if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            return ;
        }

        File file = new File(path);

        // 判断父文件是否存在，如果不存在则创建
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }

        PrintStream out = null;
        try {
            out = new PrintStream(new FileOutputStream(file));
            out.print(temp.toString());
            Toast.makeText(DataProcessingActivity.this,"保存成功",
                    Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(out!=null) {
                out.close();
            }
        }
    }

    public void load(View view) {
        try {
            FileInputStream fis = new FileInputStream(path);
            StringBuilder sb = new StringBuilder("");
            byte[] buff = new byte[1024];
            int hasRead = 0;

            while ((hasRead=fis.read(buff)) != -1) {
                sb.append(new String(buff,0,hasRead));
            }
            JSONObject jsonObject = new JSONObject(sb.toString());

            String statement = jsonObject.getString("statement");
            String answer = jsonObject.getString("answer");
            int difficulty = jsonObject.getInt("difficulty");

            Log.d(TAG, "题目：" + statement);
            Log.d(TAG, "题型：" + answer);
            Log.d(TAG, "题号：" + difficulty);

            Toast.makeText(DataProcessingActivity.this,"载入成功",
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
